<?php
namespace app\common\controller;
use think\Db;
use think\Request;
class AuthBase extends Base{
    public function _initialize(){
        parent::_initialize();
        $token = \think\Request::instance()->header('token');
        if(!empty(config('app_status'))) {
            //线上测试及正式环境
            if (!$token) {
                echo json_encode(['code' => 401, 'msg' => 'token不存在']);
                exit();
            }
            $res = $this->checkToken($token);
            if ($res == 90003) {
                echo json_encode(['code' => 401, 'msg' => 'token已过期']);
                exit();
            } else if ($res == 90002) {
                echo json_encode(['code' => 401, 'msg' => 'token无效']);
                exit();
            } else if (is_array($res)) {
                //定义一个常量，后面方便使用
                session('time_out', time() + 3600 * 12);
                defined('USER_ID') or define('USER_ID', $res["userid"]);
                defined('USER_DEPT') or define('USER_DEPT', $res["dept"]);
                defined('USER_NAME') or define('USER_NAME', $res["name"]);
                defined('IS_ADMIN') or define('IS_ADMIN', $res['isadmin']);
                $url = strtolower(request()->module()) . '/' . strtolower(request()->controller()) . '/' . request()->action();
                $result = Db::table('hs_auth_rule')->where('name', $url)->count();
                // 第一个参数是规则名称,第二个参数是用户UID
                if (!($this->auth_check($url, USER_ID, IS_ADMIN)) && $result > 0) {
                    if (\request()->isGet()) {
                        echo "没有权限";
                        exit;
                    }
                    echo json_encode(["code" => 1, "msg" => "没有权限访问"]);
                    exit;
                }
            }
        }
    }

    /*
     * 方法名：level
     * 方法说明：权限节点
     */
    public function auth_check($url,$userid=USER_ID,$isadmin = IS_ADMIN){
        $new = new \auth\Auth();
        $auth = $new->instance();
        if($auth->check($url,$userid) || $isadmin){
            return true;
        }
        return false;
    }

    public function tree(array $data, $pk = 'id', $pid = 'f_id', $child = 'child',$level = 0)
    {
        $nowData = [];
        foreach ($data as $key => $val) {
            if ( $val[$pid] == $level ) {
                unset($data[$key]);
                if ( !empty($data) ) {
                    $child = $this->tree($data,$pk,$pid,$child,$val[$pk]);
                    if ( !empty($child) ) {
                        $val['child'] =  $child;
                    }
                }
                $nowData[] = $val;
            }
        }
        return $nowData;
    }


    //检测token
    public function checkToken($token)
    {
        $info = json_decode(decode($token),true);
        if(session('time_out')){
            $time_out = session('time_out') > $info['time_out'] ? session('time_out') : $info['time_out'];
        }else{
            $time_out = $info['time_out'];
        }
        if (!empty($info)) {
            //token过期,更新token有效期
            if (time() > $time_out) {
                return 90003;
            }else{
                return $info;
            }
        }else{
            return 90002;
        }
    }
}